﻿
Namespace Forms
    Friend Class TipsForm

#Region " Fields "

        Private _Data As TipsDataSet
        Private _TipsShown As List(Of Integer)

        Private _SuppressMarkingTipAsRead As Boolean = False
        Private _DisplayMethod As TipDisplayMethod = TipDisplayMethod.Normal

#End Region

#Region " Constructors "

        ''' <summary>
        ''' Creates a new instance of the form based on the given data
        ''' </summary>
        ''' <param name="data">TipsDataSet - the instance to use as the source of Application Tips</param>
        ''' <remarks></remarks>
        Friend Sub New(ByVal data As TipsDataSet)

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
            If data Is Nothing Then _
                Throw New ArgumentNullException("data")

            _Data = data
            _TipsShown = New List(Of Integer)
        End Sub

        ''' <summary>
        ''' Creates a new instance of the form based on the given data with the TipDisplayMethod specified.
        ''' </summary>
        ''' <param name="data">TipsDataSet - the instance to use as the source of Application Tips</param>
        ''' <param name="displayMethod">TipDisplayMethod - specifies whether to display the next tip in the display order or a random tip.</param>
        ''' <remarks></remarks>
        Friend Sub New(ByVal data As TipsDataSet, ByVal displayMethod As TipDisplayMethod)
            MyClass.New(data)
            _DisplayMethod = displayMethod
        End Sub

#End Region

#Region " Properties "

        ''' <summary>
        ''' Gets or sets a value the indicates what the user's preference is for showing tips at startup.
        ''' </summary>
        Public Property ShowTipsAtStartup() As Boolean
            Get
                Return ShowTipsAtStartupCheckBox.Checked
            End Get
            Set(ByVal value As Boolean)
                ShowTipsAtStartupCheckBox.Checked = value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets a TipDisplayMethod used to specify which tip to display when the form loads.
        ''' </summary>
        Public Property DisplayMethod() As TipDisplayMethod
            Get
                Return _DisplayMethod
            End Get
            Set(ByVal value As TipDisplayMethod)
                _DisplayMethod = value
            End Set
        End Property

#End Region

#Region " Methods "

        ''' <summary>
        ''' Associates the form's binding source with the underlying data.
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub SetDataSources()

            Select Case _DisplayMethod

                Case TipDisplayMethod.Random
                    'Setting the _SuppressMArkingTipsAsRead flag is necessary as the action of setting the binding
                    'source causes the "BindingSource.CurrentChanged" event to fire which is where we keep track of what
                    'tips have been shown.  This flag prevents a tip from being marked as shown that is not actually shown.
                    _SuppressMarkingTipAsRead = True
                    Me.TipsBindingSource.DataSource = _Data
                    _SuppressMarkingTipAsRead = False

                    'Now set the position of the binding source to a random number from 1 to the number of records in the binding source.
                    Dim rnd As New Random()
                    Me.TipsBindingSource.Position = rnd.Next(1, Me.TipsBindingSource.Count)
                    ConfigurePreviousAndNextButtons()

                Case TipDisplayMethod.Normal
                    'For normal display mode, just set the data source.  Tips will naturally display in defined DisplayOrder 
                    '(based on the Sort property of the BindingSource
                    Me.TipsBindingSource.DataSource = _Data

            End Select

        End Sub

        ''' <summary>
        ''' Configures the enabled state of the Previous and Next buttons based on the position in the binding source.
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub ConfigurePreviousAndNextButtons()

            If Me.TipsBindingSource.Position = 0 Then
                Me.PreviousButton.Enabled = False
            Else
                Me.PreviousButton.Enabled = True
            End If
            If Me.TipsBindingSource.Position = Me.TipsBindingSource.Count - 1 Then
                Me.NextButton.Enabled = False
            Else
                Me.NextButton.Enabled = True
            End If

        End Sub

        ''' <summary>
        ''' Adds the given TipID to the list of tips that have been shown during this session.
        ''' </summary>
        ''' <param name="thisTipID">Integer - specifies which TipID to add to the list.</param>
        ''' <remarks></remarks>
        Private Sub AddToTipsShownList(ByVal thisTipID As Integer)

            If Not _TipsShown.Contains(thisTipID) Then
                _TipsShown.Add(thisTipID)
            End If

        End Sub

#End Region

#Region " Event Handlers "

#Region " Form Events "

        Private Sub TipsForm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            If _TipsShown IsNot Nothing AndAlso _TipsShown.Count > 0 Then
                _Data.Tips.MarkTipsAsDisplayed(_TipsShown.ToArray())
            End If
        End Sub

        Private Sub TipsForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Me.Text = String.Format("{0} Tip", My.Application.Info.ProductName)
            SetDataSources()
        End Sub

        Private Sub TipsForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            Me.BringToFront()
        End Sub

#End Region

#Region " Button Events "

        Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End Sub

        Private Sub PreviousButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreviousButton.Click
            Me.TipsBindingSource.MovePrevious()
        End Sub

        Private Sub NextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextButton.Click
            Me.TipsBindingSource.MoveNext()
        End Sub

#End Region

#Region " BindingSource Events "

        Private Sub TipsBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TipsBindingSource.CurrentChanged
            If _SuppressMarkingTipAsRead Then _
                Exit Sub

            Dim thisTip As DataRowView = CType(Me.TipsBindingSource.Current, DataRowView)
            If thisTip IsNot Nothing Then
                'Keep track of what tips have been shown so we can mark them as such when the form closes.
                Dim thisTipID As Integer = Convert.ToInt32(thisTip.Item("TipID"))
                AddToTipsShownList(thisTipID)
                ConfigurePreviousAndNextButtons()
            End If

        End Sub

#End Region

#End Region

    End Class
End Namespace